//
//  KYCustomCheckInFooterView.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/10/17.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYCustomCheckInFooterView.h"
#import "KYCheckInFooterModel.h"
#import "KYCustomButton.h"
#import "KYCalendarCellItem.h"
#import "KYCacheManager.h"
#import "KYCheckInOverallDataModel.h"
@interface KYCustomCheckInFooterView()
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *btns;
@property (nonatomic, strong) NSMutableArray *lines;
@property (nonatomic, weak) UIView *tipsView;
@property (nonatomic, weak) UILabel *tipsLabel;
@end

@implementation KYCustomCheckInFooterView

+ (instancetype)customCheckInFooterView {
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUpSubViews];
    }
    
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)decoder {
    if (self = [super initWithCoder:decoder]) {
        [self setUpSubViews];
    }
    return self;
}

- (void)setUpSubViews
{
    for (int i = 0; i < self.dataArr.count; i++) {
        KYCheckInFooterModel *model = self.dataArr[i];
        [self setUpBtnWithModel:model tag:i];
    }
    [self setUpTips];
}
- (void)setUpTips{
    //添加提示view
    UIView *tipsView = [[UIView alloc] init];
    tipsView.frame = CGRectMake(-10, -18, 60, 22);
    [self addSubview:tipsView];
    self.tipsView = tipsView;
    
    //背景图
    UIImageView *imgV = [[UIImageView alloc] init];
    imgV.image = [[UIImage ky_imageNamed:@"checkIn_cue"] resizableImageWithCapInsets:UIEdgeInsetsMake(10,10,10,10) resizingMode:UIImageResizingModeStretch];
    imgV.frame = tipsView.bounds;
    [self.tipsView addSubview:imgV];
    
    //次数机会
    UILabel *tipsLabel = [[UILabel alloc] init];
    tipsLabel.font = [UIFont systemFontOfSize:10];
    tipsLabel.textColor = [UIColor whiteColor];
    tipsLabel.text = @"1次机会";
    tipsLabel.frame = CGRectMake(0, 0, 60, 16);
    tipsLabel.textAlignment = NSTextAlignmentCenter;
    [self.tipsView addSubview:tipsLabel];
    self.tipsLabel = tipsLabel;
    [self setUpBaseData];
}
- (void)setUpBaseData{
    
}
- (void)setUpBtnWithModel:(KYCheckInFooterModel *)model tag:(int)tag {
    //1.添加底部时间按钮
    KYCustomButton *btn = [[KYCustomButton alloc] init];
    [btn setTitle:model.title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setImage:[UIImage ky_imageNamed:model.normalImage] forState:UIControlStateNormal];
    [btn setImage:[UIImage ky_imageNamed:model.selectedImage] forState:UIControlStateSelected];
    btn.tag = tag;
    [self addSubview:btn];
    [self.btns addObject:btn];
    //2.添加流程view
    if (tag != (self.dataArr.count - 1)) {
        UIProgressView *lineView = [[UIProgressView alloc] init];
        lineView.progressTintColor = [UIColor whiteColor];
        lineView.trackTintColor = KYColor(155, 54, 54, 1.0);
        [self addSubview:lineView];
        [self.lines addObject:lineView];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat margin = 10;
    CGFloat btnY = 0;
    CGFloat btnW = 20;
    CGFloat btnH = 40;
    for (int i = 0; i < self.btns.count; i++) {
        KYCustomButton *btn = self.btns[i];
        CGFloat lineW = ((self.frame.size.width - 2 * margin) - btnW * self.btns.count) / (self.btns.count - 1);
        CGFloat btnX = margin + (btnW + lineW) * i;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
    CGFloat lineH = 2;
    CGFloat lineY = (btnH * radio - lineH) * 0.5;
    CGFloat lineW = ((self.frame.size.width - 2 * margin) - btnW * self.btns.count) / (self.btns.count - 1);
    for (int j = 0; j < self.lines.count; j++) {
        UIProgressView *lineView = self.lines[j];
        CGFloat lineViewX = margin + btnW + (btnW + lineW) * j;
        lineView.frame = CGRectMake(lineViewX, lineY, lineW, lineH);
    }
    KYCalendarCellItem *item = [KYCalendarCellItem currentDayCacheModel];
    if (item) {
        [self checkInWithContinuityTime:item.continuityTime];
    }
}

#pragma mark - public
// 添加
- (void)checkInWithContinuityTime:(NSInteger)time
{
    for (int i = 0; i < time%7; i ++) {
        KYCustomButton *btn = self.btns[i];
        btn.selected = YES;
        if (i<6) {
            UIProgressView *lineView = self.lines[i];
            [lineView setProgress:1.0 animated:YES];
        }
    }
    KYCustomButton *btn = (KYCustomButton *)self.btns[time-1];
    self.tipsView.ky_centerX = btn.ky_centerX;
    self.tipsView.ky_centerY = btn.ky_centerY - 32;
}
- (void)refreshDate{
    KYCacheManager *cacheMgr = [KYCacheManager shareInstance];
    NSString *checkKey = [NSString stringWithFormat:@"KYCalendar%@",[NSDate currentDayString]];
    KYCalendarCellItem *itemNew = (KYCalendarCellItem *)[cacheMgr ky_ObjectForKey:checkKey];
    if (itemNew) {
        [self checkInWithContinuityTime:itemNew.continuityTime];
    }
}

- (void)updateTipslabel:(NSString *)str{
    self.tipsLabel.text = str;
}
#pragma mark - Lazy load
- (NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
        // 0.常规数据
        NSArray *titleArr = @[@"1天",@"2天",@"3天",@"4天",@"5天",@"6天"];
        NSString *normalImage = @"checkIn_waitCheckIn";
        NSString *selectedImage = @"checkIn_isCheckIn";
        for (int i = 0; i < titleArr.count; i++) {
            KYCheckInFooterModel *model = [KYCheckInFooterModel itemWithNormalImage:normalImage selectedImage:selectedImage title:titleArr[i]];
            [_dataArr addObject:model];
        }
        
        // 1.第7天数据
        NSString *boxImage = @"checkIn_unCheckIn";
        NSString *boxSelectedImage = @"checkIn_isCheckIn";
        KYCheckInFooterModel *model = [KYCheckInFooterModel itemWithNormalImage:boxImage selectedImage:boxSelectedImage title:@"7天"];
        [_dataArr addObject:model];
    }
    
    return _dataArr;
}

- (NSMutableArray *)btns
{
    if (_btns == nil) {
        _btns = [NSMutableArray array];
    }
    
    return _btns;
}

- (NSMutableArray *)lines
{
    if (_lines == nil) {
        _lines = [NSMutableArray array];
    }
    
    return _lines;
}

@end
